<?php
/*****************************************************

  Copyright:	(c) 2006  SeaDesign Web Studio
        Web:	www.seadesign.ru
      Email: 	seadesign@gmail.com
  
*****************************************************/

class Url
{
	function __construct($url) 
	{
		if ($url) {
			$this->urlparts = parse_url($url);
			parse_str($this->get('query'), $this->params);
		}
	}
	
	public function hasScheme() 
   {
		return $this->has('scheme');
	}
		
	public function getScheme() 
   {
		return $this->get('scheme');
	}
	
	public function hasUser() 
   {
		return $this->has('user');
	}
	
	public function getUser() 
   {
		return $this->get('user');
	}
	
	public function hasPassword() 
   {
		return $this->has('pass');
	}
	
	public function getPassword() 
   {
		return $this->get('pass');
	}
	
	public function hasHost() 
   {
		return $this->has('host');
	}
	
	public function getHost() 
   {
		return $this->get('host');
	}

	public function hasPort() 
   {
		return $this->has('port');
	}
	
	public function getPort() 
   {
		return $this->get('port');
	}
	
	public function hasPath() 
   {
		return $this->has('path');
	}
	
	public function getPath() 
   {
		return $this->get('path');
	}
	
	public function hasQuery() 
   {
		return count($this->params);
	}
	
	public function getQuery() 
   {
		return $this->buildQuery();
	}
	
	public function hasAnchor() 
   {
		return $this->has('fragment');
	}
	
	public function getAnchor() 
   {
		return $this->get('fragment');
	}
	
	public function __toString()
	{
		$url = '';
		
		if ($this->hasScheme()) {
			$url .= $this->getScheme();
			$url .= '://';
			
		}
		if ($this->hasUser()) {
			$url .= $this->getUser();
			if ($this->hasPassword()) {
				$url .= ':';
				$url .= $this->getPassword();
			}
			$url .= '@';
		}
		if ($this->hasHost()) {
			$url .= $this->getHost();
			if ($this->hasPort()) {
				$url .= ':';
				$url .= $this->getPort();
			}
		}
		if ($this->hasPath()) {
			$url .= $this->getPath();
		}
		if ($this->hasQuery()) {
			$url .= '?';
			$url .= $this->getQuery();
		}
		if ($this->hasAnchor()) {
			$url .= '#';
			$url .= $this->getAnchor();
		}

		return $url;
	}
	
	public function getParams() 
	{
		return $this->params;
	}
	
	public function hasParam($name) 
	{
		return isset($this->params[$name]);
	}

	public function getParam($name) 
	{
		return $this->params[$name];
	}
	
	public function setParam($name, $val) 
	{
		$this->params[$name] = $val;
	}
	
   public function remParam($name)
   {
      unset($this->params[$name]);
   }
   

	private function has($partName) 
	{
		return isset($this->urlparts[$partName]);
	}

	private function get($partName) 
	{
		if ($this->has($partName)) {
			return $this->urlparts[$partName];
		}
		return '';
	}

	private function set($partName, $val) 
	{
		$this->urlparts[$partName] = $val;
	}

	private function buildQuery() 
	{
		$query = '';
		foreach ($this->params as $nm => $val) 
		{
			if ($query) {
				$query .= '&';
			}
			if (is_array($val))
			{
				$query .= self::array2query($val, $nm);
			}
			else
			{
				$query .= $nm.'='.urlencode($val);
			}
		}
		return $query;
	}
	
	private static function array2query($a, $nm)
	{
		$query = '';
		foreach ($a as $k => $v)
		{
			if ($query) {
				$query .= '&';
			}
			if (is_array($v))
			{
				$query .= self::array2query($v, $nm."[$k]");
			}
			else
			{
				$query .= $nm."[$k]=".urlencode($v);
			}
		}
		return $query;
	}
	

	private $urlparts = array();
	private $params = array();
};




class Params
{
	function __construct() 
	{
		$this->pageName = $_SERVER['SCRIPT_NAME'];
		$this->remoteAddr = $_SERVER['REMOTE_ADDR'];
		$this->fileName = substr(strrchr($_SERVER['SCRIPT_NAME'],'/'),1);
		$this->pageLink = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
		
		$this->path = parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);		//URL without parameters
		
		if (!function_exists('stripslashes_recursive')) 
		{
			function stripslashes_recursive($value) 
			{
				return (is_array($value) ? 
					array_map('stripslashes_recursive', $value) : 
					stripslashes($value));
			}
		}

		
		$_GET = array_map('stripslashes_recursive', $_GET) ;
		foreach ($_GET as $nm => $val) {
			$this->set(urldecode($nm), $val);
		}
		
		$_POST = array_map('stripslashes_recursive', $_POST) ;
		foreach ($_POST as $nm => $val) {
			$this->set($nm, $val);
		}
		
		//if $_POST is suddenly empty, see if post params (WITH files) exceed 'post_max_size' 
	}
	
	public function set($nm, $val) 
	{
		$this->items[$nm] = $val;
	}
	
	public function exist($nm) 
	{
		return isset($this->items[$nm]);
	}
	
	public function get($nm) 
	{
		if (!$this->exist($nm)) {
			die("Parameter $nm not found amont send to [$this->pageName] parameters \n Params.get");
		}
		return $this->items[$nm];
	}
	
		/** @return null if param not exists or is not valid number */
	public function asInt($nm)
	{       
		if ($this->exist($nm))
		{   
			$val = $this->get($nm);
			if (strlen($val))
			{   
				return intval($val);
			}
		}              
		return null;
	}
	
		/** @return null if param not exists or empty string*/
	public function asStr($nm)
	{
		if ($this->exist($nm))
		{
			$s = $this->get($nm);   
			if (strlen($s))                  
			{                                
				return $s;                    
			}                                
		}                                   
		return null;                        
	}
	
	public function remove($nm) 
	{
		$oldval = null;
		if ($this->exist($nm)) {
			$oldval = $this->get($nm);
		}
		unset($this->items[$nm]);
		return $oldval;
	}
	
	public function asArray() 
	{
		return $this->items;
	}

	
	public function toArray() 
	{
		return $this->items;
	}
	
	public function count() 
	{
		return count($this->items);
	}
	
	public function getPageName() 
	{
		return $this->pageName;
	}

   /**
      @return  page name without path and extension
   */
   public function getBarePageName()
   {
      $matches = array();
      if (preg_match("!([^/]+)\\.[a-zA-Z]+$!",$this->getPageName(), $matches)) {
         return $matches[1];
      }
      return "";
   }
	
	public function getFileName() 
	{
		return $this->fileName;
	}
	
	public function getPageLink() 
	{
		return $this->pageLink;
	}
	
	public function getReferer()
	{
		$referer = $this->asStr('referer');
		if (!$referer)
		{
			return $_SERVER['HTTP_REFERER'];
		}
		return $referer;
	}
	
	public function getPath() 
	{
		return $this->path;
	}


	public function href() 
	{
		return $this->asHref($this->items);
	}
	
	public function hrefWithout()
	{
		$items = $this->items;
		
		$numargs = func_num_args();
		for ($i=0; $i<$numargs; ++$i) {
			$param = func_get_arg($i);
			unset($items[$param]);
		}
		
		return $this->asHref($items);
	}
	
	public function hrefWith($params)		//input array parname => parvalue
	{
		$items = $this->items;
		foreach ($params as $nm => $val) {
			$items[$nm] = $val;
		}
		
		return $this->asHref($items);
	}

   public function asString()
   {
      return $this->__toString();
   }

	public function __toString() 
	{
		$s = '';
		foreach ($this->items as $nm => $val) 
		{
			if (!empty($s)) {
				$s .= '&';
			}
			$s .= "$nm=$val";
		}
		return $s;
	}

	
 				//CGI Environment Variables (hoohoo.ncsa.uiuc.edu/cgi/env.html)
	private $pageName = null;
	private $remoteAddr = null;
	private $fileName = null;
	private $path = null;
	private $pageLink = null;

		

	private function asHref($items) 		//returns the entire URL as a strings
	{
		$href = '';
		if (count($items)) {
			foreach ($items as $nm => $val) {
				if (!empty($href)) {
					$href .= '&';
				}
				$href .= "$nm=$val";
			}	
		}
		else {
			return $this->pageName;
		}
		
		return $this->pageName . '?' . $href;
	}

	private $items = array();
};

$par = new Params;

?>